package com.cljz.makepassenger.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.BaseActivity;
import com.cljz.makepassenger.model.net.bean.GetBillListBean;
import com.cljz.makepassenger.present.activity.MyWalletP;
import com.cljz.makepassenger.ui.adapter.TransactionListAdapter;
import com.cljz.makepassenger.utils.L;
import com.jakewharton.rxbinding.view.RxView;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;

import static com.cljz.makepassenger.ui.activity.SettingPwdActivity.TYPE_SET;

public class MyWalletActivity extends BaseActivity<MyWalletP> implements XRecyclerView.LoadingListener, TransactionListAdapter.OnItemClickListener {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title_name)
    TextView tvTitleName;
    @BindView(R.id.fl_title_right)
    FrameLayout flTitle;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.btn_withdraw)
    Button btnWithdraw;
    @BindView(R.id.xrv_transactions)
    XRecyclerView xrvTransaction;
    private PopupWindow popupWindow;
    private int page = 1;
    private boolean isRefresh = false;
    private boolean isLoadMore = false;
    private TransactionListAdapter transactionListAdapter;


    @Override
    public void initData(Bundle savedInstanceState) {
        tvTitleName.setText("我的钱包");
        ivTitleRight.setImageResource(R.mipmap.wallet_more);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        xrvTransaction.setLayoutManager(linearLayoutManager);
        transactionListAdapter = new TransactionListAdapter(this);
        xrvTransaction.setAdapter(transactionListAdapter);
        xrvTransaction.setLoadingMoreProgressStyle(ProgressStyle.BallClipRotate);
        xrvTransaction.setPullRefreshEnabled(true);
        xrvTransaction.setLoadingMoreEnabled(true);

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_wallet;
    }

    @Override
    public MyWalletP newP() {
        return new MyWalletP();
    }

    @Override
    protected void initListener() {

        RxView.clicks(ivBack)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        finish();
                    }
                });
        RxView.clicks(flTitle)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        if (getP().checkPwd()) {
                            showSettingWindow();
                        } else {
                            showSettingPwd();
                        }
                    }
                });
        RxView.clicks(btnWithdraw)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        if (getP().checkPwd()) {
                            Router.newIntent(MyWalletActivity.this).putString("allMoney", tvBalance.getText().toString().trim()).to(WithdrawActivity.class).launch();
                        } else {
                            showSettingPwd();
                        }
                    }
                });
        xrvTransaction.setLoadingListener(this);
        xrvTransaction.refresh();
        transactionListAdapter.setOnItemClickListener(this);
    }

    private void showSettingPwd() {
        Router.newIntent(this).putInt("type", TYPE_SET).to(SettingPwdActivity.class).launch();
    }

    private void showSettingWindow() {
        View view = getLayoutInflater().inflate(R.layout.pop_wallet_setting, null);
        View cancel = view.findViewById(R.id.btn_cancel);
        View modify = view.findViewById(R.id.btn_modify);

        popupWindow = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        popupWindow.setOutsideTouchable(true);
        setBackgroundAlpha(0.5f);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1f);
            }
        });
        popupWindow.showAtLocation(findViewById(R.id.root_view), Gravity.BOTTOM, 0, 0);

        RxView.clicks(cancel)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        if (popupWindow != null)
                            popupWindow.dismiss();
                    }
                });

        RxView.clicks(modify)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        if (popupWindow != null)
                            popupWindow.dismiss();
                        Router.newIntent(MyWalletActivity.this).putInt("type", SettingPwdActivity.TYPE_VERIFY).to(SettingPwdActivity.class).launch();
                    }
                });
    }

    //设置半透明的效果
    private void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha;
        if (bgAlpha == 1) {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
        } else {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        getWindow().setAttributes(lp);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
        }
    }

    public void getBillListSucceed(GetBillListBean.DataBean data) {
        tvBalance.setText(data.getUserMoney());
        L.e("1_net:" + data.getBillList().size());
        if (isRefresh) {
            xrvTransaction.setNoMore(false);
            transactionListAdapter.addData(data.getBillList(), isRefresh);
            xrvTransaction.refreshComplete();
            isRefresh = false;
        } else if (isLoadMore) {
            transactionListAdapter.addData(data.getBillList(), isRefresh);
            if (data.getBillList().size() < 10) {
                xrvTransaction.setNoMore(true);
            } else {
                xrvTransaction.setNoMore(false);
            }
            xrvTransaction.loadMoreComplete();
            isLoadMore = false;
        }
    }

    public void getBillListFail(String error) {
        L.e(error);
        xrvTransaction.refreshComplete();
        xrvTransaction.loadMoreComplete();
    }

    @Override
    public void onRefresh() {
        isRefresh = true;
        page = 1;
        getP().getBillList(page);
    }

    @Override
    public void onLoadMore() {
        isLoadMore = true;
        page++;
        getP().getBillList(page);
    }

    @Override
    public void onItemClick(ArrayList<GetBillListBean.DataBean.BillListBean> data, int position) {
        if ("2".equals(data.get(position).getStatus()) || "4".equals(data.get(position).getStatus())) {
            if (data.get(position).isTurnShow()) {
                data.get(position).setTurnShow(false);
            } else {
                data.get(position).setTurnShow(true);
            }
            transactionListAdapter.setDataWithChangePosition(data, position + 1);
        }
    }
}
